﻿using System.Collections.Generic;
using System.Configuration;
using System.Xml;

namespace HostServicePhoneCommands.AppConfig
{
    public class ServiceSettingsHandler : IConfigurationSectionHandler
    {
        #region IConfigurationSectionHandler Members

        public object Create(object parent, object configContext, XmlNode section)
        {
            ICollection<ServiceSetting> services = new List<ServiceSetting>();
            foreach (XmlNode item in section.ChildNodes)
            {
                var alias = "";
                if (item.Attributes != null && item.Attributes["alias"] != null)
                {
                    alias = item.Attributes["alias"].Value;
                }

                var enabled = false;
                if (item.Attributes != null && item.Attributes["enabled"] != null)
                {
                    enabled = bool.Parse(item.Attributes["enabled"].Value);
                }
                if (item.Attributes != null && item.Attributes["autostart"] != null)
                {
                }
                services.Add(new ServiceSetting
                                 {
                                     Assembly = item.Attributes != null?item.Attributes["assembly"].Value:string.Empty,
                                     ClassName = item.Attributes != null?item.Attributes["className"].Value:string.Empty,
                                     Enabled = enabled,
                                     Alias = alias
                                 });
            }
            return services;
        }

        #endregion
    }
}